package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanPageQueryDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;


    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private NursingProjectMapper nursingProjectMapper;


    /**
     * 分页条件查询护理计划信息
     *
     * @param dto
     * @return
     */
    @Override
    public PageBean<NursingPlanVo> pageQuery(NursingPlanPageQueryDto dto) {
//        1.设置分页参数
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());

//        2.查询护理计划
        Page<NursingPlanVo> page = nursingPlanMapper.pageQuery(dto);

        return PageBean.of(page, NursingPlanVo.class);
    }


    /**
     * 新增护理计划
     *
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addNursingPlan(NursingPlanDto dto) {
//        1.dto转pojo
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
//        2.补充一些参数
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setCreateBy(1671403256519078239L);
        nursingPlan.setUpdateBy(1671403256519078239L);
        nursingPlan.setStatus(1);
//        3.新增护理计划
        nursingPlanMapper.insert(nursingPlan);

        List<NursingProjectPlan> nursingProjectPlans = BeanUtil.copyToList(dto.getProjectPlans(), NursingProjectPlan.class);


//        前端传输数据有个默认值，size大于等于1，必进集合遍历，但有没有项目ID不一定
        if(CollUtil.isEmpty(nursingProjectPlans)){
            return;
        }

        nursingProjectPlans.forEach(nursingProjectPlan -> {

            nursingProjectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlan.setCreateTime(LocalDateTime.now());
            nursingProjectPlan.setUpdateTime(LocalDateTime.now());
            nursingProjectPlan.setCreateBy(1671403256519078239L);
            nursingProjectPlan.setUpdateBy(1671403256519078239L);
        });
//        4.新增中间表
        nursingProjectPlanMapper.insert(nursingProjectPlans);


    }


    /**
     * 根据ID查询护理计划
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo selectNursingPlan(Long id) {
//        1.查到护理计划表
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);


//        2.查到护理计划中间表
        Long nursingPlanId = nursingPlan.getId();
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlanMapper.getByPlanId(nursingPlanId);

        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);


        return nursingPlanVo;
    }


    /**
     * 修改护理计划
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setUpdateBy(1671403256519078239L);
        nursingPlanMapper.update(nursingPlan);

        // 先删除再添加
        nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());

        // 再新增
        List<NursingProjectPlan> nursingProjectPlans = BeanUtil.copyToList(dto.getProjectPlans(), NursingProjectPlan.class);
        nursingProjectPlans.forEach(nursingProjectPlan -> {
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlan.setCreateTime(LocalDateTime.now());
            nursingProjectPlan.setUpdateTime(LocalDateTime.now());
            nursingProjectPlan.setCreateBy(1671403256519078239L);
            nursingProjectPlan.setUpdateBy(1671403256519078239L);
        });
        nursingProjectPlanMapper.insert(nursingProjectPlans);

    }

    /**
     * 删除护理计划和中间表
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNursingPlan(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
//        1.数据有问题、不存在不可以删除
        if(nursingPlan == null) {
            throw  new RuntimeException("查询不到对应护理项目，请求参数非法,");
        }
//        2.状态处于启用不可删除
        if(nursingPlan.getStatus() == 1){
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }


        nursingPlanMapper.deleteById(id);
        nursingProjectPlanMapper.deleteByPlanId(id);
    }

    /**
     * 启用禁用状态
     *
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        nursingPlan.setStatus(status);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setUpdateBy(1671403256519078239L);
        nursingPlanMapper.update(nursingPlan);
    }

    /**
     * 获取所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.list();
        List<NursingPlanVo> nursingPlanVos = BeanUtil.copyToList(nursingPlans, NursingPlanVo.class);
        return nursingPlanVos;
    }


}

